//纵向堆叠柱状图---图例下钻
//drillValue-boolean类型，true表示图例下钻，false表示不下钻
//drllUrl--drillValue下钻的要跳转的url地址
function DRAWPATAITSTACK(url,modelSend,drawContainer,drillValue,drillUrl) {
    this.draw = function () {
        console.log(modelSend);
        $.ajax({ type: "post",
            url: url,
            dataType: "json",
            data: {"paramData":JSON.stringify(modelSend)},
            success: function (result) {
                console.log(result);
                var str="";
                if(result.front){
                    for (var i=0;i<result.front.length;i++){
                        str += '<p class="titleSub">' + result.front[i].label + '' +
                            '<span>' + result.front[i].value + '</span></p>';
                    }
                    $("#summarizing").html(str);
                }
                $("#text_h4").html(result.titledate+"月");
                var dcInstance= new DRAWSTACK(result,drawContainer,drillValue,drillUrl,JSON.stringify(modelSend));
                dcInstance.draw(result,drawContainer,drillValue,drillUrl,JSON.stringify(modelSend));
            },
            error: function (err) {
                console.log("服务器链接失败");
            }});
    };
};
//定义一个用于绘制图形的类
function DRAWSTACK(json,drawContainer,drillValue,drillUrl,modelSend){
    //坐标轴的颜色
    this.colorg=['#357FB7','#D95550','#9AC062'];
    //根据得到的数据绘制Y轴
    this.setYaxis=function(data){
        var yaxArray=[];
        for(var i=0;i<data.axis.yCnt;i++){
            var k=i;
            var obj={labels:{
                    formatter:function(){
                        return Highcharts.numberFormat(this.value,0,".",",");
                    },
                    style:{color:''}},
                title:{text:''}};
            obj.labels.style.color =this.colorg[k];
            if(data.theme.yAxis[k].align=='right'||data.theme.yAxis[k].opposite=='false'){
                obj.labels.formatter=function(){
                    return Highcharts.numberFormat(this.value,0,".",",")+"%";
                };
                obj.opposite='true';
            }
            yaxArray.push(obj);
        }
        return yaxArray;
    };
    //根据得到的数据设置指标
    this.setSeries=function(dataSource){
        var serArray=[];
        for(var key in dataSource.data){
            var obj={};
            var mun=dataSource.data[key].yAxis;
            obj.type=dataSource.theme.yAxis[mun].type=='line'?'line':'column';
            obj.name=dataSource.data[key].label;
            obj.data=dataSource.data[key].value;
            obj.color=this.colorg[key];
            obj.zIndex=dataSource.theme.yAxis[mun].type=='line'?1:0;
            obj.yAxis=dataSource.data[key].yAxis;
            obj.dataLabels={enabled: false,//堆图默认提示文字
                color: '#000000'};
            serArray.push(obj);
        }
        return serArray;
    };
    //绘制堆叠柱状图的方法
    this.draw=function(json,container){
        var option={
            chart: {
                type: 'column' , //纵向堆图
                padding:[20,10,80,10]
            },
            title: {
                text: ''     // 标题
            },
            xAxis: {
                categories: json.axis.x   // x 轴分类
            },
            yAxis:this.setYaxis(json),
            tooltip: {
                shared: true,
                useHTML:true,
                pointFormatter:function(){
                    return '<table style="width: 140px">' +
                        '<tr><td style="padding:0;">'+this.series.name+':</td>' +
                        ' <td style="text-align: right"><b style="padding:0;">'+ Highcharts.numberFormat(this.y,0,".",",")+'('+Highcharts.numberFormat(this.percentage,0,".",",")+'%)</b></td></tr></table>';
                }
            },
            plotOptions: {
                column: {stacking: 'normal'},
                series:{
                    events:{
                        legendItemClick:function(e){
                            if(drillValue) {
                                var lab=e.target.name;
                                console.log(e.target);
                                var str=drillUrl+'?lab='+lab+'&modelSend='+modelSend;
                                window.open(str,'_self');
                            };
                            /*var index = this.index;
                            var series = this.chart.series;
                            if (series[index].visible) {
                                for (var i = 0; i < series.length; i++) {
                                    var s = series[i];
                                    i === index ? s.show() : s.hide();
                                }
                            }
                            return false;*/
                        }

                    }
                }

            },
            legend:{
                align : 'center',
                verticalAlign : 'bottom',
                itemWidth:300,
                maxHeight:200
            },
            credits: {//隐藏官网提示
                enabled: false
            },
            exporting: {
                chartOptions: {
                    plotOptions: {
                        series: {
                            dataLabels: {
                                enabled: true
                            }
                        }
                    }
                }
            },
            series:this.setSeries(json)
        };
        Highcharts.setOptions({
            lang: {printChart:"打印图表",
                downloadJPEG: "下载JPEG 图片" ,
                downloadPDF: "下载PDF文档"  ,
                downloadPNG: "下载PNG 图片"  ,
                downloadSVG: "下载SVG 矢量图" ,
                exportButtonTitle: "导出图片"
            }
        });
        Highcharts.chart(container,option);
    };
}